package com.huixi.service.impl;

import com.huixi.config.properties.JwtProperties;
import com.huixi.entity.User;
import com.huixi.service.AuthService;
import com.huixi.service.UserService;
import com.huixi.utils.JwtUtils;
import com.huixi.common.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "authService")
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public String authentication(String username, String password) throws Exception {
        User user = userService.queryUser(username, password);
        if (user == null) {
            return null;
        }

        UserInfo userInfo = new UserInfo(user.getId(), user.getUsername());
        return JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
    }
}
